package com.hefeng.example.controller;

import com.hefeng.example.domain.CustomerManualWorkOrder;
import com.hefeng.example.domain.WorkOrderDispositionResult;
import com.hefeng.example.service.CustomerManualWorkOrderService;
import com.hefeng.example.service.SichuangResultService;
import com.hefeng.example.service.WorkOrderDispositionResultService;
import com.hefeng.common.core.utils.EmptyUtils;
import com.hefeng.common.core.utils.file.FileUtils;
import com.hefeng.common.core.utils.oss.AliOssUtil;
import com.hefeng.common.core.web.domain.AjaxResult;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.*;

@Slf4j
@RestController
@RequestMapping("/result")
@Api(tags = "工单结果接口")
public class WorkOrderDispositionResultController {

    @Autowired
    private WorkOrderDispositionResultService workOrderDispositionResultService;
    @Autowired
    private SichuangResultService sichuangResultService;
    @Autowired
    private CustomerManualWorkOrderService customerManualWorkOrderService;


    /**
     * 通过id查工单处置结果
     *
     * @param workId
     * @return
     */
    @PostMapping("WordResultByID")
    public AjaxResult slelctById(Integer workId) {
        return AjaxResult.success(workOrderDispositionResultService.queryById(workId));
    }

    /**
     * 获取现场图片url
     *
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/pictureurl", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    public AjaxResult getUrl(@RequestParam(value = "file") MultipartFile file) {
        WorkOrderDispositionResult picture1 = new WorkOrderDispositionResult();
        AliOssUtil aliOssUtil = new AliOssUtil();//
        File fl = MultipartFileToFile(file);//将MultipartFile转换为File
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddhhmmss");
        String pictureName = sdf.format(new Date()) + ".jpg";//生成图片名称
        String path = aliOssUtil.uploadFile(fl, "scene/pic/" + pictureName);
        System.out.println(path);
        picture1.setLivePhotos(path);
        return AjaxResult.success("上传成功", path);//封装json
    }


    /**
     * 获取图片或者视频url
     *
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/AutographUrl", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    public AjaxResult getAutographUrl(@RequestParam(value = "file") MultipartFile[] files) {
        List<Map> list = new ArrayList<>();
        try {
            if (EmptyUtils.isEmpty(files)) {
                System.out.println("文件为空！");
                return AjaxResult.error("文件不能为空", false);
            }

            for (MultipartFile file : files) {
                Map map = new HashMap<>();
                String fileName = file.getOriginalFilename();  // 文件名
                String filePath = "";
                if (FileUtils.isImageFile(fileName.substring(fileName.indexOf("."), fileName.length()))) {
                    filePath = "pic";
                }
                if (fileName.substring(fileName.indexOf("."), fileName.length()).equals(".mp4")) {
                    filePath = "video";
                }

                //创建输出文件对象
                String fname = UUID.randomUUID().toString() + FileUtils.getName(fileName);
                AliOssUtil ossUtil = new AliOssUtil();
                String url = ossUtil.uploadFile(file.getInputStream(), "scene/pic/" + filePath + "/" + fname);
                map.put("url", url);
                list.add(map);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return AjaxResult.success("上传成功", list);
    }

    /**
     * 将MultipartFile转换为File
     *
     * @param multiFile
     * @return
     */
    public static File MultipartFileToFile(MultipartFile multiFile) {
        // 获取文件名
        String fileName = multiFile.getOriginalFilename();
        // 获取文件后缀
        String prefix = fileName.substring(fileName.lastIndexOf("."));
        // 若须要防止生成的临时文件重复,能够在文件名后添加随机码

        try {
            File file = File.createTempFile(fileName, prefix);
            multiFile.transferTo(file);
            return file;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 新增工单处置结果
     *
     * @param
     * @return
     */
    @ResponseBody
    @PostMapping("insertWorkResult")

    public AjaxResult insertFile(WorkOrderDispositionResult workOrderDispositionResult) {

        String userType = workOrderDispositionResult.getUserType();
        boolean isZhuanpai = Boolean.parseBoolean(workOrderDispositionResult.getIsZhuanpai());
        //判断用户类型 01维保  02四川合丰
        switch (userType) {
            case "02":
                workOrderDispositionResult.setDispositionStatus(String.valueOf(1));
                if (isZhuanpai == true) {
                    workOrderDispositionResult.setDispositionStatus(String.valueOf(2));
                    CustomerManualWorkOrder customerManualWorkOrder = new CustomerManualWorkOrder();
                    // customerManualWorkOrder.setId(workOrderDispositionResult.getDispatchId());
                    customerManualWorkOrder.setCreatedUser(workOrderDispositionResult.getDispatchId());
                    customerManualWorkOrder.setDeviceCode(workOrderDispositionResult.getDeviceCode());
                    customerManualWorkOrder.setWorkName(workOrderDispositionResult.getWorkName());
                    customerManualWorkOrder.setWorkDescription(workOrderDispositionResult.getWorkDescription());
                    customerManualWorkOrder.setLiveVideo(workOrderDispositionResult.getLiveVideo());
                    customerManualWorkOrder.setLivePhotos(workOrderDispositionResult.getLivePhotos());
                    BeanUtils.copyProperties(workOrderDispositionResult, customerManualWorkOrder);
                    customerManualWorkOrder.setId(workOrderDispositionResult.getWorkId());
                    customerManualWorkOrderService.insert(customerManualWorkOrder);
                }
                sichuangResultService.insertSICHUANG(workOrderDispositionResult);
                break;
            case "01":
                workOrderDispositionResultService.insertR(workOrderDispositionResult);
                break;
        }

        return AjaxResult.success("工单处理完成");
    }

}
